package ink.xiaobaibai.emqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-06-15
 **/

@Slf4j
public abstract class MyMqttCallback implements MqttCallback {

    private EMqttService eMqttService;

    protected abstract void connectionLost2(Throwable throwable);

    protected abstract void messageArrived2(String s, MqttMessage mqttMessage);

    protected abstract void deliveryComplete2(IMqttDeliveryToken iMqttDeliveryToken);

    @Override
    public void connectionLost(Throwable throwable) {
        this.connectionLost2(throwable);
        if (this.eMqttService.isShutdown()) {
            log.info("主动关闭");
            return;
        }
        //重启
        this.eMqttService.restart();
        log.error("重启成功");
    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        this.messageArrived2(s, mqttMessage);
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        this.deliveryComplete2(iMqttDeliveryToken);
    }

    public void seteMqttService(EMqttService eMqttService) {
        this.eMqttService = eMqttService;
    }

    public EMqttService geteMqttService() {
        return this.eMqttService;
    }
}
